🚀 Implement dynamic status messages from JSON file#2192
Conversation
There was a problem hiding this comment.
Pull request overview
This PR refactors the bot's status messages from hardcoded strings to a dynamic system loaded from a JSON file. The change enables easier maintenance and updates of status messages without code changes, following the same pattern used for token complaints in the codebase.
Changes:
- Added new
status_messages.gofile with thread-safe loading and retrieval of status messages from JSON - Integrated status messages into the data download/reload system in
tasks.go - Removed 37 hardcoded status message strings from
main.goand replaced with dynamic loading - Added file watching for the status messages JSON file to enable hot-reloading
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/ei/status_messages.go | New file implementing thread-safe loading and random selection of status messages from JSON |
| src/tasks/tasks.go | Added constants and integration for downloading/loading status messages alongside other data files |
| main.go | Removed hardcoded status message array, added dynamic loading at startup and file watching for hot-reload |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| log.Printf("Failed to open status messages file: %v", err) | ||
| return | ||
| } | ||
| defer func() { | ||
| if err := file.Close(); err != nil { | ||
| log.Printf("Failed to close: %v", err) | ||
| } | ||
| }() | ||
|
|
||
| var messagesLoaded StatusMessagesFile | ||
| decoder := json.NewDecoder(file) | ||
| if err := decoder.Decode(&messagesLoaded); err != nil { | ||
| log.Printf("Failed to decode status messages: %v", err) | ||
| return |
There was a problem hiding this comment.
The error handling in LoadStatusMessages differs from the similar LoadTokenComplaints function in the same package (src/ei/token_complaints.go, line 28). LoadTokenComplaints uses log.Fatal when the file cannot be opened, which ensures the application doesn't continue with missing data during startup. LoadStatusMessages logs the error and returns, which could result in an empty StatusMessages slice.
Since both functions are called during initialization (main.go line 142 and tasks.go line 366), consider using log.Fatal for consistency, or document why the different behavior is intentional. If status messages are optional, this should be clearly indicated.
| log.Printf("Failed to open status messages file: %v", err) | |
| return | |
| } | |
| defer func() { | |
| if err := file.Close(); err != nil { | |
| log.Printf("Failed to close: %v", err) | |
| } | |
| }() | |
| var messagesLoaded StatusMessagesFile | |
| decoder := json.NewDecoder(file) | |
| if err := decoder.Decode(&messagesLoaded); err != nil { | |
| log.Printf("Failed to decode status messages: %v", err) | |
| return | |
| log.Fatalf("Failed to open status messages file %q: %v", filename, err) | |
| } | |
| defer func() { | |
| if err := file.Close(); err != nil { | |
| log.Printf("Failed to close status messages file %q: %v", filename, err) | |
| } | |
| }() | |
| var messagesLoaded StatusMessagesFile | |
| decoder := json.NewDecoder(file) | |
| if err := decoder.Decode(&messagesLoaded); err != nil { | |
| log.Fatalf("Failed to decode status messages from file %q: %v", filename, err) |
| "encoding/json" | ||
| "fmt" | ||
| "log" | ||
| "math/rand" |
There was a problem hiding this comment.
The import uses math/rand (v1) while the rest of the codebase has migrated to math/rand/v2. This is inconsistent with the pattern used in src/tasks/tasks.go (line 7), src/boost/boost.go (line 10), src/boost/boost_admin.go (line 9), src/boost/contract.go (line 7), and src/boost/roles.go (line 7), which all use math/rand/v2.
Using the v2 API is recommended for Go 1.22+ as it provides better random number generation and doesn't require manual seeding. Change the import to use math/rand/v2 for consistency with the rest of the codebase.
| "math/rand" | |
| "math/rand/v2" |
No description provided.